package com.like.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.like.entity.CourseInfo;
import com.like.entity.M;
import com.like.hospitalworld.R;
import com.like.utils.DateConverter;
import com.like.utils.ImageUtils;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class VideoCourseAdapter extends BaseAdapter {
    private List<CourseInfo> courseInfos;
    private M m;

    public VideoCourseAdapter(M m, List<CourseInfo> courseInfos) {
        this.m = m;
        this.courseInfos = courseInfos;
        if (courseInfos == null)
            this.courseInfos = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return courseInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return courseInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Object obj = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_course, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();
        CourseInfo courseInfo = (CourseInfo) obj;
        holder.sContent.setText(courseInfo.title);
        holder.sDate.setText(DateConverter.convertShitToDate(courseInfo.updateTime, "yyyy/MM/dd"));
        holder.sWatchCount.setText(courseInfo.readNum + "");
        ImageUtils.showImage(m.mContext, courseInfo.titlePage, m.getToken(), holder.sImg);
        return convertView;
    }

    public class ViewHolder {
        @ViewInject(R.id.item_video_date)
        public TextView sDate;
        @ViewInject(R.id.item_video_watch_count)
        public TextView sWatchCount;
        @ViewInject(R.id.item_video_content)
        public TextView sContent;
        @ViewInject(R.id.img)
        public ImageView sImg;

        public ViewHolder(View convertView) {
            x.view().inject(this, convertView);
        }
    }

    public void update(List<CourseInfo> infos, boolean append) {
        if (!append)
            this.courseInfos = infos;
        else
            this.courseInfos.addAll(infos);
        this.notifyDataSetChanged();
    }
}
